package com.diandian.backend.store.controller;

import com.diandian.backend.store.param.*;
import com.diandian.backend.store.service.ElemeRetailStoreTreeService;
import com.diandian.base.boot.starter.response.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * [门店]饿了么零售门店组织树API
 *
 * @author zyz diandian@qq.com
 * @since 1.0.0 2025-08-25
 */
@RestController
@RequestMapping("/eleme/retail/store/tree")
public class ElemeRetailStoreTreeController {

    @Autowired
    private ElemeRetailStoreTreeService elemeRetailStoreTreeService;

    /**
     * 查询整个组织树：单层
     * @param param
     * @return
     */
    @PostMapping("/query")
    public BaseResult query(@RequestBody StoreElemeRetailStoreTreeQueryParam param) {
        param.check();
        return new BaseResult(0, "success", elemeRetailStoreTreeService.queryTree(param.toDTO()));
    }

    /**
     * 查询整个组织树：多层
     * @param param
     * @return
     */
    @PostMapping("/query/multi")
    public BaseResult queryMultiTree(@RequestBody StoreElemeRetailStoreTreeQueryParam param) {
        param.check();
        return new BaseResult(0, "success", elemeRetailStoreTreeService.queryMultiTree(param.toDTO()));
    }

    /**
     * 添加节点
     * 说明：分区下不能再建分区
     * @param param
     * @return
     */
    @PostMapping("/add")
    public BaseResult addNode(@RequestBody StoreElemeRetailStoreTreeAddParam param) {
        param.check();
        elemeRetailStoreTreeService.addNode(param.toDTO());
        return new BaseResult(0, "success", null);
    }

    /**
     * 更新节点基本信息
     * 说明：分区下不能再建分区
     * @param param
     * @return
     */
    @PostMapping("/update")
    public BaseResult updateNode(@RequestBody StoreElemeRetailStoreTreeUpdateByIdParam param) {
        param.check();
        elemeRetailStoreTreeService.updateNode(param.toDTO());
        return new BaseResult(0, "success", null);
    }

    /**
     * 查询节点详情
     * @param param
     * @return
     */
    @PostMapping("/detail")
    public BaseResult getNodeDetail(@RequestBody StoreElemeRetailStoreTreeDetailParam param) {
        return new BaseResult(0, "success", elemeRetailStoreTreeService.getDetail(param.toDTO()));
    }

    /**
     * 删除节点（存在下级节点和存在关联门店的不能删除）
     * @param param
     * @return
     */
    @PostMapping("/delete")
    public BaseResult deleteNode(@RequestBody StoreElemeRetailStoreTreeDeleteByIdParam param) {
        param.check();
        elemeRetailStoreTreeService.deleteNode(param.toDTO());
        return new BaseResult(0, "success", null);
    }

    /**
     * 修改节点父级
     * 说明：分区下不能再建分区
     * @param param
     * @return
     */
    @PostMapping("/adjustNode")
    public BaseResult adjustNode(@RequestBody StoreElemeRetailStoreTreeAdjustParam param) {
        param.check();
        elemeRetailStoreTreeService.adjustNode(param.getId(), param.getTargetParentNode());
        return new BaseResult(0, "success", null);
    }
}